package com.juyu.miliao.api.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.security.core.Authentication;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.annotation.JsonView;
import com.juyu.miliao.api.domain.UserPhoto;
import com.juyu.miliao.api.service.UserPhotoService;
import com.juyu.miliao.api.util.JacksonView;
import com.juyu.miliao.api.validator.UserPhotoCheck;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.IllegalParamException;

/**
 * 照片墙接口
 * 
 * @author wbf
 */
@RestController
@RequestMapping("/userPhoto")
public class UserPhotoController {

	@Resource
	UserPhotoService userPhotoService;

	/**
	 * 获取用户照片墙
	 * @param id  -userId
	 * @return
	 */
	@GetMapping("/{id}")
	@JsonView(JacksonView.UserPhoto.class)
	public List<UserPhoto> getUserPhotoById(Authentication token,@PathVariable("id") String id) {
		if(id==null || id.equals("null") || id.equals("") || id.equals("(null)")){
			Subject subject = (Subject) token.getPrincipal();
			id = subject.getId()+"";
		}
		return userPhotoService.getUserPhotoById(Long.parseLong(id));
	}

	/**
	 * 保存用户照片墙
	 * 
	 * @param userId
	 * @return
	 */
	@PatchMapping()
	public void updateUserPhoto(Authentication token, @Validated(UserPhotoCheck.class) UserPhoto entity) {
		Subject subject = (Subject) token.getPrincipal();
		if (subject.getId() != entity.getUserId())
			throw new IllegalParamException("用户id错误");
		userPhotoService.updateUserPhoto(entity);
	}

}
